<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1 id="info"></h1>
    <button id="btn1">开始</button>
    <button id="btn2">暂停</button>
    <script>
        var oInfo = document.getElementById('info')
        var btn1 = document.getElementById("btn1");
        var btn2 = document.getElementById("btn2");

        var a = 0;
        //设置一个全局变量
        var timer;

        btn1.onclick = function() {
            //为了防止定时器叠加 我们应该在设置定时器之前清除定时器
            clearInterval(timer)
                //更改全局变量的值为一个定时器实体
            timer = setInterval(function() {
                oInfo.innerText = ++a;
            }, 1000)
        }

        btn2.onclick = function() {
            clearInterval(timer)
        }
    </script>
</body>

</html>